<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\services\car;


use app\dao\car\BaseLogDao;
use app\model\car\BaseLog;
use app\services\BaseServices;
use think\facade\Db;
/**
 * Class BaseLogServices
 * @package app\services\shendeng
 */
class BaseLogServices extends BaseServices
{
    public function __construct(BaseLogDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page, $limit] = $this->getPageValue();
        }
        $list = $this->dao->getList($where,$page,$limit);
        $obj_type = [
            1 => '用户操作',
            2 => '订单操作',
            3 => '每日价格',
        ];
        $op_user_type = [
            1 => '用户',
            2 => '店员',
            3 => '后台人员',
            4 => '系统',
        ];
        $operation_type = [
            1 => '用户提交订单',
            2 => '取消订单',
            3 => '用户支付订单',
            4 => '用户申请退款',
            5 => '用户完成取车',
            6 => '用户完成还车',
            7 => '订单续租',
            8 => '续租订单退款',
            9 => '取消续租订单',
            10 => '每日价格添加',
            11 => '每日价格编辑',
            12 => '每日价格删除',
            13 => '创建单面收费',
            14 => '续租订单审核',
            15 => '订单退款',
            16 => '退款驳回',
            17 => '完成订单',
            18 => '订单换车',
            19 => '支付续租订单',
            20 => '提交保养表单',
            21 => '后台指派订单',
            22 => '后台完成订单',
            23 => '后台取消订单',
            24 => '预约处理',
            25 => '押金支付',
            26 => '押金退款',
            27 => '分账',
            28 => '车辆押金T+1退款',
            29 => '违章押金T+15退款',
        ];
        foreach ($list['list'] as &$item){
            $item['obj_type'] = $item['obj_type'] ? $obj_type[$item['obj_type']] : '未知';
            $item['op_user_type'] = $item['op_user_type'] ? $op_user_type[$item['op_user_type']] : '未知';
            $item['operation_type'] = $item['operation_type'] ? $operation_type[$item['operation_type']] : '未知';
            $item['operation_time'] = $item['operation_time']!==0 ? date('Y-m-d H:i:s',$item['operation_time']) : '';
        }
        return $list;
    }
    /**
     * 添加日志
     * @param int $obj_id 对象id
     * @param int $obj_type 对象类型：1用户操作，2订单操作，3每日价格 4分账操作
     * @param string $log_content 日志说明
     * @param int $op_user_type 操作人类型： 1 用户 2店员 3后台人员 4系统
     * @param int $operation_user_id 操作人id
     * @param string $operation_user_name 操作人姓名
     * @param int $operation_type 操作类型 1 用户提交订单，2取消订单，3，用户支付订单，4用户申请退款，5用户完成取车，6用户完成还车，7订单续租，8续租订单退款，9取消续租订单
     * 10，每日价格添加，11，每日价格编辑，12，每日价格删除，13创建单面收费，14续租订单审核，15订单退款，16退款驳回，17完成订单，18订单换车，19支付续租订单，20提交保养表单
     * 21，后台指派订单，22，后台完成订单，23，后台取消订单，24预约处理，25押金支付，26押金退款，27订单分账，28退车辆押金，29退违章押金
     * @return bool
     */
    public static function addLog(int $obj_id,int $obj_type,string $log_content,int $op_user_type,int $operation_user_id,string $operation_user_name,int $operation_type){
        $data = [
            'obj_id'=>$obj_id,
            'obj_type'=>$obj_type,
            'log_content'=>$log_content,
            'op_user_type'=>$op_user_type,
            'operation_user_id'=>$operation_user_id,
            'operation_user_name'=>$operation_user_name,
            'operation_type'=>$operation_type,
            'operation_time'=>time()
        ];
        $model = new BaseLog();
        $res = $model->save($data);
        return $res;
    }

    /**
     * 添加押金退款日志
     * @return bool
     */
    public static function add_deposit_Log($order_id,$order_no,$type,$y_amount,$t_amount,$k_amount,$notes,$store_id,$cs_amount,$css_amount)
    {
        $data = [
            'order_id'=>$order_id,
            'order_no'=>$order_no,
            'type'=>$type,
            'add_time'=>time(),
            'y_amount'=>$y_amount,
            't_amount'=>$t_amount,
            'k_amount'=>$k_amount,
            'notes'=>$notes,
            'store_id'=>$store_id,
            'cs_amount'=>$cs_amount,
            'css_amount'=>$css_amount
        ];
        $res = Db::name('deposit_record')->insert($data);
        return $res;
    }

    //添加支付成功订单流水 生成1,2,3,6
    public static function add_order_flow($orderInfo){
        $user = Db::name('user')->where('uid',$orderInfo['uid'])->find();
        $place = Db::name('base_place')->where('id',$orderInfo['store_id'])->find();
        $zhoumo_price = Db::name('car_type')->where('id',$orderInfo['type_id'])->value('zhoumo_price');
        $order= [
            'store_id'=>$orderInfo['store_id'],
            'order_id'=>$orderInfo['id'],
            'uid'=>$orderInfo['uid'],
            'add_time'=>time(),
            'flow_account'=>$place['sub_mchid'],
            'account'=>$place['name'],
            'status'=>1,
        ];
        //新增订单款流水
        $order['amount'] = $orderInfo['price'];
        $order['type'] = 1;
//        $order['remarks'] =$user['nickname']."支付订单".$orderInfo['order_no'].",单价：".$orderInfo['car_fee']."/天，"."租赁天数共：".$orderInfo['book_day']."天";
//        $order['remarks'] = "支付订单".$orderInfo['order_no'].",单价：".$orderInfo['car_fee']."/天，"."租赁天数共：".$orderInfo['book_day']."天，". "租车服务费：".$orderInfo["base_service_fee"]."元，"."租车总押金：".$orderInfo["yajin"]."元，"."优惠券抵扣：".$orderInfo["coupon_price"]
//            ."元，"."订单原价：".$orderInfo['old_price']."元，"."订单总价：".$orderInfo['price']."元";
        if($orderInfo["base_service_fee"]==""){
            $orderInfo["base_service_fee"] = 0;
        }
        $order['remarks'] = "支付订单".$orderInfo['order_no'].",平日价：".$orderInfo['car_price']."/天，"."周末价：".$zhoumo_price."/天，"."租赁天数共：".$orderInfo['book_day']."天，". "租车服务费：".$orderInfo["base_service_fee"]."元，"."租车总押金：".$orderInfo["yajin"]."元，".
            "车辆押金：".$orderInfo["deposit"]."元，"."违章押金：".$orderInfo["illegal_deposit"]."元，"."优惠券抵扣：".$orderInfo["coupon_price"]
            ."元，"."订单原价：".$orderInfo['old_price']."元，"."订单总价：".$orderInfo['price']."元";
        Db::name('order_flow')->insert($order);
        //新增订单车辆押金流水
//        $order['amount'] = $orderInfo['deposit'];
//        $order['type'] = 2;
//        $order['remarks'] =$user['nickname']."支付订单".$orderInfo['order_no']."，车辆押金：".$orderInfo['deposit']."元";
//        Db::name('order_flow')->insert($order);
//        //新增订单违章押金流水
//        $order['amount'] = $orderInfo['illegal_deposit'];
//        $order['type'] = 3;
//        $order['remarks'] =$user['nickname']."支付订单".$orderInfo['order_no']."，违章押金：".$orderInfo['illegal_deposit']."元";
//        Db::name('order_flow')->insert($order);
//        //判断是否新增基础服务费流水
//        if($orderInfo['base_service_fee']){
//            $order['amount'] = $orderInfo['base_service_fee'];
//            $order['type'] = 6;
//            Db::name('order_flow')->insert($order);
//        }
        return true;
    }


    //添加订单分账流水 生成4
    public static function add_order_fz_flow($orderInfo,$msg,$fzq){
        $user = Db::name('user')->where('uid',$orderInfo['uid'])->find();
        $order= [
            'store_id'=>$orderInfo['store_id'],
            'order_id'=>$orderInfo['id'],
            'uid'=>$orderInfo['uid'],
            'add_time'=>time(),
            'flow_account'=>"1652272678",//分账都是分给服务商
            'account'=>"湖南快乐车行露营地投资发展有限公司",
            'status'=>1,
        ];
        //新增订单分账流水
        $order['amount'] = $fzq;
        $order['type'] = 4;
        $order['remarks'] =$msg;
        Db::name('order_flow')->insert($order);
        return true;
    }

    //添加退车辆押金流水 生成11,21,31
    public static function add_order_deposit_flow($type,$orderInfo,$msg,$t)
    {
        $user = Db::name('user')->where('uid', $orderInfo['uid'])->find();
        $order = [
            'store_id'=>$orderInfo['store_id'],
            'order_id' => $orderInfo['id'],
            'uid' => $orderInfo['uid'],
            'add_time' => time(),
            'flow_account' => $user['phone'],
            'account' => $user['nickname'],
            'status' => 1,
        ];
        if($type=='car'){//车辆押金
            $order['amount'] = $t;
            $order['type'] = 21;
            $order['remarks'] =$msg;
        }elseif($type=='wz'){//违章押金
            $order['amount'] = $t;
            $order['type'] = 31;
            $order['remarks'] =$msg;
        }else{//取消订单退款
            $order['amount'] = $t;
            $order['type'] = 11;
            $order['remarks'] =$msg."（包含车辆押金，违章押金）";
        }
        Db::name('order_flow')->insert($order);
        return true;

    }
    /**
     * 添加押金退款失败日志
     * @return bool
     */
    public static function add_deposit_error($order_id,$msg,$type){
        $data = [
            'order_id'=>$order_id,
            'msg'=>$msg,
            'add_time'=>time(),
            'type'=>$type
        ];
        $res = Db::name('deposit_error')->insert($data);
        return $res;
    }
        /**
     * 获取订单日志
     * @param string $obj_id
     * @param int $obj_type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLog(string $obj_id, int $obj_type ){
        $model = BaseLog::alias('l')->where('obj_id','in', $obj_id)->where('obj_type', $obj_type)->select();
        $list = $model ? $model->toArray() : [];
        if ($list) {
            $op_user_type = [
                1 => '用户',
                2 => '店员',
                3 => '后台人员',
                4 => '系统',
            ];
            foreach ($list as &$row) {
                $row['op_user_type_name'] = $op_user_type[$row['op_user_type']];
                $row['operation_time'] = $row['operation_time'] ? date('Y-m-d H:i:s',$row['operation_time']) : '暂无';
            }
            unset($row);
        }
        return $list;
    }


    /**
     * 获取订单流水
     * @param string $obj_id
     * @param int $obj_type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getFlow(string $obj_id, int $obj_type ){
        $data = Db::name('order_flow')
            ->alias('f')
            ->field('f.remarks,f.id as f_id,f.order_id as f_order_id,f.type as f_type,f.amount as f_amount,f.add_time as f_add_time,f.account as f_account,o.illegal_deposit,o.deposit,o.shifu_car_fee,o.coupon_price,o.order_no,o.pay_time,o.type_name,o.contact_name,
            o.contact_mobile,o.add_time,o.start_time,o.end_time,p.title as store_name,o.pay_time,o.price,t.car_no,o.book_day as tianshu')
            ->order('f.id desc')
            ->join('car_order o','f.order_id = o.id','left')
            ->join('car_order_take t','t.id=o.take_id','left')
            ->join('base_place p','p.id=o.store_id','left')
            ->where('order_id',$obj_id)
            ->select()->toArray();
        foreach ($data as $k=>$v) {
            $data[$k]['add_time'] = $v['add_time'] ? date('Y年m月d日 H时i分', $v['add_time']) : '暂无';
            $data[$k]['pay_status'] = $v['pay_time'] ? '已支付' : '未支付';
            $data[$k]['start_time'] = $v['start_time'] ? date('m月d日 H时i分', $v['start_time']) : '暂无';
            $data[$k]['end_time'] = $v['end_time'] ? date('m月d日 H时i分', $v['end_time']) : '暂无';
            $data[$k]['time'] = date('m月d日 H时i分', $v['start_time']);
            $data[$k]['time2'] = date('m月d日 H时i分', $v['end_time']);
            $data[$k]['f_add_time'] = $v['f_add_time'] ? date('Y年m月d日 H时i分s秒', $v['f_add_time']) : '暂无';
            $data[$k]['pay_time'] = $v['pay_time'] ? date('Y年m月d日 H时i分s秒', $v['pay_time']) : '暂无';
            if($v['f_type']==1){
                $data[$k]['f_typename'] = '订单款';
            }elseif($v['f_type']==21){
                $data[$k]['f_typename'] = '车辆押金退款';
            }elseif($v['f_type']==31){
                $data[$k]['f_typename'] = '违章押金退款';
            }elseif($v['f_type']==21){
                $data[$k]['f_typename'] = '车辆押金退款';
            }elseif($v['f_type']==2){
                $data[$k]['f_typename'] = '车辆押金';
            }elseif($v['f_type']==3){
                $data[$k]['f_typename'] = '违章押金';
            }elseif($v['f_type']==4){
                $data[$k]['f_typename'] = '分账款';
            }elseif($v['f_type']==5){
                $data[$k]['f_typename'] = '续租款';
            }elseif($v['f_type']==6){
                $data[$k]['f_typename'] = '服务费';
            }else{
                $data[$k]['f_typename'] = '取消订单退款';
            }
        }
        return $data;
    }

}
